<?php  
	namespace Home\Controller;
	use Think\Controller;
	//角色管理
	//@author  程传民
	class RoleController extends AllowController{

		//角色管理
		public function index(){
			$mod = D("role");
			$arrs['id'] = array("GT",1);
			// $arr1['id'] = array("");
			$tot = $mod->where($arrs)->count();
			$nums = 5;
			$page = new \Think\Page($tot,$nums);

	    	$list = $mod->where($arrs)->limit($page->firstRow,$page->listRows)->select();

	    	// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);
			$this->assign("list",$list);
			$this->display("Role/role");
		}

		//加载修改角色的模板
		public function edit(){
			$mod = D("role");
			$arr = array("id"=>I("get.id"));
			$list = $mod->where($arr)->find();
			$this->assign("list",$list);
			$this->display("Role/update");
		}

		//修改人员的角色
		public function update(){
			$arr = array("id"=>I("post.ids"));
			$data['name'] = I("post.rolename");
			$data['remark'] = I("post.remark");
			$mod = D("role");
			$info = $mod->where($arr)->save($data);
			if($info){
				echo "修改成功";
			}else{
				echo "修改失败";
			}
		}


		//加载为指定角色修改权限节点模板
		public function assignpower(){
			$role = D("role");

			$nodes = D("nodes");

			$rolenode = D("rolenode");

			//获取该角色的权限
			$ro_node = $rolenode->where(array('rid'=>array('eq',$_GET['id'])))->select();
			$ro_nodes = array();
			//遍历重组数组
			foreach($ro_node as $v){
				$ro_nodes[] = $v['nid'];
			}


			$info = $nodes->order("controllerName desc")->select();

			$arr = array("id"=>$_GET['id']);
			$list = $role->where($arr)->find();

			$this->assign("list",$list);
			$this->assign("ro_nodes",$ro_nodes);
			$this->assign("info",$info);
			$this->display("Role/assign");
		}


		//为角色分配权限节点
		public function doassign(){
			//获取checkbox的name

			if(empty($_POST['node'])){
				$this->error("必须选择一个节点！");
			}

			$rid = $_POST['id'];

			//删除该 角色的 所有信息--避免重复添加
			$rolenode = D("rolenode");

			$rolenode->where(array("rid"=>$_POST['id']))->delete();

			//循环添加
			foreach($_POST['node'] as $k=>$v){
				$data['nid'] = $v;
				$data['rid'] = $rid;
				//执行添加
				$rolenode->data($data)->add();
			}

			$this->success("权限修改成功！",U('Role/index'));

		}

		//删除指定角色
		public function delete(){
			$arr = array("id"=>I("post.id"));
			$mod = D("role");
			//首先判断这个角色下是否还存在用户，如果有用户，则这个角色不能被删除
			$users = D("users");
			$arrs = array("role_id"=>I("post.id"));

			$list = $users->where($arrs)->select();

			if($list){
				echo "失败";
			}else{
				$info = $mod->where($arr)->delete();
				echo "成功";
			}
		}

		//加载添加角色模板
		public function add(){
			$this->display("Role/add");
		}

		//添加一个角色
		public function insert(){
			$mod = D("role");
			// $data = I("get.name");
			$data['name'] = I("post.rolename");
			$data['remark'] = I("post.remark");
			$info = $mod->add($data);
			if($info){
				echo "添加成功";
			}else{
				echo "0";
			}
		}




	}

?>